package com.zzh.myapplication.tools;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;

public class DialogUtils {
	
	/**
	 * 定制对话框
	 * @param activity
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static PromptDialog getDefaultDialog(final Activity activity, String title, String message,
			PromptDialog.OnClickListener listener) {
		if(activity == null || activity.isFinishing()) {
			return null;
		}
		PromptDialog.Builder dialog =  new PromptDialog.Builder(activity);
		dialog.setTitle(title);
		dialog.setMessage(message);
		if(listener == null) {
			listener = new PromptDialog.OnClickListener() {
	            @Override
	            public void onClick(Dialog dialog, int which) {
	                dialog.dismiss();
					dialog = null;
	            }
	        };
		}
		dialog.setButton1("确定", listener);
		return dialog.show();
	}
	
	public static PromptDialog  getConfirmDialog(final Activity activity, String title, String message,
			PromptDialog.OnClickListener positiveListener, PromptDialog.OnClickListener negativeListener) {
		if(activity == null || activity.isFinishing()) {
			return null;
		}
		PromptDialog.Builder dialog =  new PromptDialog.Builder(activity);
		dialog.setTitle(title);
		dialog.setMessage(message);
		if(positiveListener == null) {
			positiveListener = new PromptDialog.OnClickListener() {
	            @Override
	            public void onClick(Dialog dialog, int which) {
	                dialog.dismiss();
					dialog = null;
	            }
	        };
		}
		if(negativeListener == null) {
			negativeListener = new PromptDialog.OnClickListener() {
	            @Override
	            public void onClick(Dialog dialog, int which) {
	                dialog.dismiss();
					dialog = null;
	            }
	        };
		}
		dialog.setButton1("确定", positiveListener);
		dialog.setButton2("取消", negativeListener);
		return dialog.show();
	}
	
	/**
	 * 调用系统弹出框
	 * @param activity
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static AlertDialog  getSysDefaultDialog(final Activity activity, String title, String message,
			DialogInterface.OnClickListener listener) {
		if(activity == null) {
			return null;
		}
		Builder dialog = new Builder(activity);
		dialog.setTitle(title);
		dialog.setMessage(message);
		if (listener == null) {
			listener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					dialog = null;
				}
			};
		}
		dialog.setPositiveButton("确定", listener);
		return dialog.show();
	}
	
	/**
	 * 调用系统弹出框（ConfirmDialog）
	 * @param activity
	 * @param title
	 * @param message
	 * @param positiveListener
	 * @param negativeListener
	 * @return
	 */
	public static AlertDialog  getSysConfirmDialog(final Activity activity, String title, String message,
			DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) {
		Builder dialog = new Builder(activity);
		dialog.setTitle(title);
		dialog.setMessage(message);
		if (positiveListener == null) {
			positiveListener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					dialog = null;
				}
			};
		}
		if (negativeListener == null) {
			negativeListener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					dialog = null;
				}
			};
		}
		dialog.setPositiveButton("确定", positiveListener);
		dialog.setNegativeButton("取消", negativeListener);
		return dialog.show();
	}
}
